package com.ljl;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/26 9:08
 */
public class MinaServer {

    public static void main(String[] args) throws IOException {
        int port = 7036;
        NioSocketAcceptor socketAcceptor = new NioSocketAcceptor();
        //保持tcp连接
        socketAcceptor.getSessionConfig().setKeepAlive(true);
        //设置编解码器
        socketAcceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                        new TextLineCodecFactory(
                                Charset.forName("gbk"),
                                LineDelimiter.WINDOWS.getValue(),
                                LineDelimiter.WINDOWS.getValue()
                        )
                ));
        //消息过滤器
        socketAcceptor.getFilterChain().addLast("stringFilter", new StringFilter());
        //设置消息处理器
        socketAcceptor.setHandler(new ServerHandler());
        //侦听端口
        socketAcceptor.bind(new InetSocketAddress(port));
        System.out.println("侦听端口：" + port);
    }

}
